"""
Case Type   : 兼容M*数据库maketime函数
Case Name   : subtime函数，返回值测试，若越界，合理报错
Create At   : 2022.11.25
Owner       : z@wan005
Description :
    1、入参1为time类型，返回结果超[-838:59:59, 838:59:59]
    2、入参1为time类型，返回结果等于左右边界
    3、入参1为datetime类型，返回结果超范围或时间不合法
    4、入参1为datetime类型，返回结果等于右边界
    5、返回结果时间为24点
    6、清理环境
Expect      :
    1、报错
    2、成功
    3、部分测试点报错
    4、成功
    5、报错
    6、成功
History     : modified by zhanghuan96，2024.06.28，优化断言
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest05(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_subtime_0005"

    def test_utc_date(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:入参1为time类型，返回结果超[-838:59:59, 838:59:59];' \
               'expect:报错--'
        self.log.info(text)
        sql_cmd = "select subtime(8385959,'-1:00:00');" \
                  "select subtime(8375959.9999999,'-1:00:00');" \
                  "select subtime(-8385959,'1:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        res_list = ['838:59:59', '838:59:59', '-838:59:59']
        for res in res_list:
            self.assertIn(res, sql_res, '执行失败' + text)

        text = '--step2:入参1为time类型，返回结果等于左右边界;expect:成功--'
        self.log.info(text)
        sql_cmd = "select subtime(8375959,'-1:00:00');" \
                  "select subtime(-8375959,'1:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 9, '行数不到9行')
        self.assertEqual('838:59:59', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('-838:59:59', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step3:入参1为datetime类型，返回结果超范围或时间不合法;' \
               'expect:部分测试点报错--'
        self.log.info(text)
        sql_cmd = "select subtime('0000-01-01 23:59:59', '23:59:59');" \
                  "select subtime('9999-12-31 23:59:59', '-01:00:00');" \
                  "select subtime" \
                  "('9999-12-31 00:00:00', '-23:59:59.9999995');" \
                  "select subtime('9999-12-31 23:00:00', '-1:00:00');" \
                  "select subtime('9999-12-30 24:00:00', '-24:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 13, '行数不到13行')
        self.assertEqual(0, len(sql_res.splitlines()[2].strip()),
                         '执行失败' + text)
        self.assertEqual(3, sql_res.count('WARNING:  date/time field overflow'),
                         '执行失败' + text)
        self.assertIn('ERROR:  date/time field value out of range: '
                      '"9999-12-30 24:00:00"', sql_res, '执行失败' + text)

        text = '--step4:入参1为datetime类型，返回结果等于右边界;expect:成功--'
        self.log.info(text)
        sql_cmd = "select subtime('9999-12-31 23:59:59.999999', '-00:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 4, '行数不到4行')
        self.assertEqual('9999-12-31 23:59:59.999999',
                         sql_res.splitlines()[2].strip(),
                         '执行失败' + text)

        text = '--step5:返回结果时间为24点;expect:报错--'
        self.log.info(text)
        sql_cmd = "select subtime('9999-12-30 24:00:00', '-00:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        msg = sql_res.splitlines()
        self.log.info(msg)
        self.assertIn('ERROR:  date/time field value out of range', sql_res,
                      '执行失败' + text)

    def tearDown(self):
        text = '----step6:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
